<template>
  <DataManageTable
    ref="tableRef"
    :tableConfig="tableConfig"
    @getTableData="getTableData"
    @saveRowEvent="saveRowEvent"
  />
</template>

<script setup>
import { getTableDataAsync, saveOrUpdateAsync } from '@/api/miniProgramConfig/articleInfo'
import { useDataManage } from '@/hooks'
import tableConfig from './config/table.config'

const tableRef = ref()
const { getTableData, saveRowEvent } = useDataManage({
  getTableDataAsync,
  saveOrUpdateAsync,
  saveParamsCallBack,
  tableRef
})

function saveParamsCallBack(updateRow, row) {
  let params = null
  const { id, title, url, imgUrl, itemId, sort } = updateRow.length ? updateRow[0] : row
  params = {
    title,
    url,
    imgUrl,
    itemId,
    sort
  }
  // 有id表示是老数据，否则是新数据
  if (Object.hasOwn(row, 'id')) params.id = id
  return params
}
</script>
